package com.jsu.springboot_crop.controller;

import com.github.pagehelper.PageInfo;
import com.jsu.springboot_crop.common.Result;
import com.jsu.springboot_crop.common.StatusCode;
import com.jsu.springboot_crop.entity.Finance;
import com.jsu.springboot_crop.entity.FinanceUserDetails;
import com.jsu.springboot_crop.entity.Recommend;
import com.jsu.springboot_crop.service.BankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wang
 * @Date 2023/8/28 14:53
 */
@RestController
@RequestMapping("/finance")
public class BankFinanceController {
    @Autowired
    BankService bankService;

    @Value("${bank.bankid}")
    private Integer bankid;
    //查询推荐融资人
    @GetMapping("/selectRecommend/{pageNum}")
    public Result selectRecommend(@PathVariable("pageNum")Integer pageNum){
        PageInfo<Recommend> recommenders = bankService.selectRecommend(pageNum);
        return new Result(true, StatusCode.OK,"查询成功",recommenders);
    }
    //根据银行id查询融资情报
    @GetMapping("/getByBankId/{bankId}")
    public Result getByBankId(@PathVariable("bankId") Integer bankId){
        List<Finance> finances = bankService.selectByBankId(bankId);
        return new Result(true, StatusCode.OK, "查询成功",finances);
    }
    //根据银行id查询融资情报
    @GetMapping("/selectByBankId/{pageNum}")
    public Result<PageInfo> selectByBankId(@PathVariable("pageNum") Integer pageNum) {
        Finance financeKey = new Finance();
        financeKey.setBankId(bankid);
        PageInfo<Finance> financePageInfo = bankService.selectByFinance(financeKey,pageNum);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", financePageInfo);
    }
    //根据银行id与申请人查询融资情报
    @GetMapping("/selectByIdName/{id}/{name}")
    public Result selectByIdName(@PathVariable("id") Integer id,@PathVariable("name") String name){
        List<Finance> finances = bankService.selectByIdName(id,name);
        return new Result(true, StatusCode.OK, "查询成功",finances);
    }
    //根据银行id与申请人查询融资情报
    @GetMapping("/selectByFinance/{keys}/{pageNum}")
    public Result<PageInfo> selectByFinance(@PathVariable("keys") String keys, @PathVariable("pageNum") Integer pageNum) {
        Finance financeKey = new Finance();
        financeKey.setBankId(bankid);
        PageInfo<Finance> financePageInfo = bankService.selectByFinance(financeKey,pageNum);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", financePageInfo);
    }
    //根据id查询融资情报
    @GetMapping("/selectByFinanceId/{id}")
    public Result selectFinanceById(@PathVariable("id") Integer id){
        Finance finance = bankService.selectFinanceById(id);
        return new Result(true, StatusCode.OK, "查询成功",finance);
    }
    //根据id修改融资情报
    @PutMapping("/updateByFinance")
    public Result updateByFinance(@RequestBody Finance finance){
        bankService.updateByFinance(finance);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    //单人贷款
    @GetMapping("/selectFinaceUser/{bankId}")
    public Result selectFinaceUser(@PathVariable("bankId") Integer bankId){
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        FinanceUserDetails financeUserDetails = bankService.selectFinanceUser(name,bankId);
        return new Result(true, StatusCode.OK, null,financeUserDetails);
    }

    //根据银行id查询银行名称
    @GetMapping("/selectBankName/{bankId}")
    public Result selectBankName(@PathVariable("bankId") Integer bankId) {
        String bankName = bankService.selectBankName(bankId);
        return new Result(true, StatusCode.OK, "查询成功",bankName);
    }
}
